﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.XmlDataModel;
using System.Collections.ObjectModel;
using System.Xml;
using SharedLib.Helpers;

namespace Tracy.DataModel
{
    //TODO: ID
    public abstract class XmlDataModelWithAliases : XmlDataModelWithAttributesAndID, IHasAliases
    {
        public XmlDataModelWithAliases(XmlNode node) : base(node) { }
        public XmlDataModelWithAliases() : base() { }

        Collection<string> _aliases;

        public Collection<string> Aliases
        {
            get
            {
                if (_aliases == null) _aliases = new Collection<string>();
                return _aliases;
            }
        }

        protected override XmlElement GenerateXmlNode(XmlDocument doc)
        {
            XmlElement rootNode = base.GenerateXmlNode(doc);
            if (Aliases.Count > 0)
            {
                XmlElement aliasesNode = doc.CreateElement("aliases");
                foreach (string alias in Aliases)
                {
                    aliasesNode.AppendChild(XmlHelper.CreateElementWithTextValue("alias", alias, doc));
                }
                rootNode.AppendChild(aliasesNode);
            }
            return rootNode;
        }

        protected override void ParseRootNode(XmlElement rootNode)
        {
            base.ParseRootNode(rootNode);
            foreach (string alias in XmlHelper.GetNodeValues(rootNode, "aliases/alias"))
            {
                Aliases.Add(alias);
            }
        }


        abstract public string Name
        {
            get;
            set;
        }
    }
}
